import flash.display.BitmapData;
import flash.geom.Matrix;


// Spot style - 0
// print style - 1
// cItem style - 2
var renderLayer:MovieClip;
// var selectedRenderLayer:MovieClip;

function initialize(evtTargetArr:Array)
{
	for (var i=0; i<evtTargetArr.length; i++)
	{
		addEvent(evtTargetArr[i]);
	}
}

function addEvent(target:MovieClip)
{
	target.onRollOver=onRollOverEvent;
	target.onRollOut=onRollOutEvent;
	target.onPress=onPressEvent;
	target.onRelease=onReleaseEvent;
	target.onReleaseOutside=onRollOutEvent;
}

function checkSceneTransitionProcess():Boolean
{
	return Packages.system.display.SceneTransition.processFlag;
}

function onRollOverEvent()
{
	if (checkSceneTransitionProcess()) return;
	
	var rst=checkType(this._name);
	if (rst==0)
	{
		renderLayer=this._parent.attachMovie("underline", "spotStyle", this._parent.getNextHighestDepth());
		renderLayer._width=this._width*2;
		renderLayer._x=this._x;
		renderLayer._y+=this._y+this._height;
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("line");
	}
	else if (rst==1)
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("print");
		this._alpha=50;
	}
}

function onRollOutEvent()
{
	if (checkSceneTransitionProcess()) return;
	
	var rst=checkType(this._name);
	if (rst==0)
	{
		renderLayer.unloadMovie();
		renderLayer.removeMovieClip();
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("normal");
	}
	else if (rst==1)
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouse("pen");
		this._alpha=100;
	}
}

function onPressEvent()
{
	if (checkSceneTransitionProcess()) return;
	
	var rst=checkType(this._name);
	if (rst==0)
	{
		renderLayer.removeMovieClip();
		renderLayer=this._parent.attachMovie("undercircle", "spotStyle", this._parent.getNextHighestDepth());
		renderLayer._height=this._height;
		renderLayer._width=this._width;
		renderLayer._x=this._x+renderLayer._width/2;
		renderLayer._y+=this._y+renderLayer._height/2;
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("circle");
	}
	else if (rst==1)
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("drop");
	}
}

function onReleaseEvent()
{
	// if (checkSceneTransitionProcess()) return;
	
	var rst=checkType(this._name);
	if (rst==0)
	{
		renderLayer.removeMovieClip();
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("down");
	}
	else if (rst==1)
	{
		Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("raise");
	}

	Packages.system.mlf.GateKeeper.dispatchMessage({type:"onReleaseButtonEvent", param:this._name, target:this});
}

function checkType(name:String):Number
{
	if (name.indexOf("Spot")>-1)
	{
		return 0;
	}
	else if (name.indexOf("print")>-1)
	{
		return 1;
	}
	else return -1;
}

function invokeRollOverEvent(param, data, slot)
{
	if (checkSceneTransitionProcess()) return;
	
	var target=mlf.mcmanager.PathResolver.extractPath(param);
	
	renderLayer=target._parent.attachMovie("underline", "@spotStyle", target._parent.getNextHighestDepth());
	renderLayer._width=target._width*2;
	renderLayer._x=target._x;
	renderLayer._y=target._y+target._height;
	Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("line");
}

function invokeRollOutEvent(param, data, slot)
{
	if (checkSceneTransitionProcess()) return;
	
	renderLayer.unloadMovie();
	renderLayer.removeMovieClip();
	Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("normal");
}

function invokePressEvent(param, data, slot)
{
	if (checkSceneTransitionProcess()) return;
	
	var target=mlf.mcmanager.PathResolver.extractPath(param);
	
	renderLayer.removeMovieClip();
	renderLayer=target._parent.attachMovie("undercircle", "@spotStyle", target._parent.getNextHighestDepth());
	renderLayer._height=target._height;
	renderLayer._width=target._width;
	renderLayer._x=target._x+renderLayer._width/2;
	renderLayer._y=target._y+renderLayer._height/2;
	Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("circle");
}

function invokeReleaseEvent(param, data, slot)
{
	// if (checkSceneTransitionProcess()) return;
	
	var target=mlf.mcmanager.PathResolver.extractPath(param);

	// selectedRenderLayer.removeMovieClip();
	if (target._parent._parent["@selectedSpotStyle"]!=null)
	{
		target._parent._parent["@selectedSpotStyle"].removeMovieClip();
	}

	/* // duplicateMovieClip method
	var selectedRenderLayer=renderLayer.duplicateMovieClip("@selectedSpotStyle", target._parent.getNextHighestDepth());
	selectedRenderLayer.gotoAndStop(35);
	target._parent._parent.selectedSpotRef=selectedRenderLayer;
	*/
	// /* // attachMovie method
	var selectedRenderLayer=target._parent._parent.attachMovie("undercircle", "@selectedSpotStyle", target._parent._parent.getNextHighestDepth(), {_x:target._x+target._width/2+target._parent._x, _y:target._y+target._height/2+target._parent._y, _width:target._width, _height:target._height});
	selectedRenderLayer.gotoAndStop(35);
	// */

	// target._parent._parent.selectedSpotRef=selectedRenderLayer;

	renderLayer.removeMovieClip();

	Packages.system.events.ThemeMouseEvent.changeAttachedMouseStyle("down");
	Packages.system.mlf.GateKeeper.dispatchMessage({type:"onReleaseButtonEvent", param:target._name, target:target, data:data});
}

function createSelectedSpot(target)
{
	removeSelectedSpot(target);
	
	var selectedRenderLayer=target._parent.attachMovie("undercircle", "@selectedSpotStyle", target._parent.getNextHighestDepth(), {_x:target._x+target._width/2, _y:target._y+target._height/2, _width:target._width, _height:target._height});
	selectedRenderLayer.gotoAndStop(35);
}

function removeSelectedSpot(target)
{
	if (target._parent["@selectedSpotStyle"]!=null)
	{
		target._parent["@selectedSpotStyle"].removeMovieClip();
	}
}